/**
 * @param {number} x
 * @return {number}
 */
var reverse = function(x) {
  let s = x.toString().split("").reverse();
  let negative = false;
  if(s[s.length - 1] == "-") {
    negative = true;
    s.length = s.length - 1;
  }
  let result = null;
  if(negative) {
    result = -parseInt(s.join(""));
  } else {
    result = parseInt(s.join(""));
  }
  if(result >= -(2 ** 31) && result <= (2 ** 31 - 1)) {
    return result;
  } else {
    return 0;
  }
};
let x = 123;
console.log(reverse(x));
x = -123;
console.log(reverse(x));
x = 120;
console.log(reverse(x));
x = 0;
console.log(reverse(x));
// let x = 123456;
// let a = x.toString().split("");
// console.log(a);
